<?php
# file that will process and create in database new meeting proposal

# include of site functions
include_once $_SERVER["DOCUMENT_ROOT"].'Meet-n-go/php/includes.php'; 	// contains includes of all files with functions
session_start();					// starts session for current user
c_db_connect(); 					// connect to database

# make sure all data was entered

if(!isset($_POST['place']) || empty($_POST['place'])) i_set_msg_err('Empty place');
else{
	$place = c_escape($_POST['place']); // escape before doing something with it
}

if(!isset($_POST['duration']) || empty($_POST['duration'])) i_set_msg_err('Empty duration');
else{
	$duration = c_escape($_POST['duration']); // escape before doing something with it
}

if(!isset($_POST['min_date']) || empty($_POST['min_date'])) i_set_msg_err('Empty minimal date');
else{
	$min_date = c_escape($_POST['min_date']); // escape before doing something with it
}

if(    !isset($_POST['min_time']) 
	|| (empty($_POST['min_time']) && $_POST['min_time'] != '0')) i_set_msg_err('Empty minimal hour');
else{
	$min_time = c_escape($_POST['min_time']); // escape before doing something with it
}

if(!isset($_POST['max_date']) || empty($_POST['max_date'])) i_set_msg_err('Empty maximal date');
else{
	$max_date = c_escape($_POST['max_date']); // escape before doing something with it
}

if(!isset($_POST['max_time']) 
	|| (empty($_POST['max_time']) && $_POST['max_time'] != '0')) i_set_msg_err('Empty maximal hour');
else{
	$max_time = c_escape($_POST['max_time']); // escape before doing something with it
}

if(!isset($_POST['group_id']) || empty($_POST['group_id'])) i_set_msg_err('Empty passing group id');
else{
	$group_id = c_escape($_POST['group_id']); // escape before doing something with it
}

if( !has_errors() ){
	
	$minimal_time = $min_date.' '.$min_time.':00:00';
	$maximal_time = $max_date.' '.$max_time.':00:00';
	$duration = intval($duration);
	$group_id = intval($group_id);
	
	$query = "INSERT INTO prop_meeting (PLACE, MIN_TIME, MAX_TIME, GROUP_ID, DURATION) 
					VALUES ('$place', '$minimal_time', '$maximal_time', $group_id, $duration)";
	if(!c_db_query($query)) i_set_msg_err('Error creating new meeting'); // problem with database
	
}

c_db_disconnect(); 					// disconnect from database

# Where to go after processing
if( has_errors() ) 	$page_id = I_CREATE_MEETING.'&grp='.$group_id; // if there are erros, go back to meeting creation page
else 				$page_id = I_GROUP_OVERVIEW.'&grp='.$group_id; // if there are no erros, go back to group overview page


# code that makes sure, this page is being redirected from afterwards
echo '<head><meta http-equiv="refresh" content="0; url=../../../index.php?id=' . $page_id . '" /></head>';


?>